<template>
	<view class="bookstore">
		<view class="bg">
			<view class="top">
				<view class="title">書架</view>
				<view class="edit" @click="goEditbook()">
					<image src="../../static/edit.png" mode="widthFix"></image>
				</view>
			</view>
			<view class="con">
				<view class="left">
					<view class="con-top">
						<view class="num">
							0
						</view>
						<view>
							分鐘
						</view>
					</view>
					<view style="font-size: 24rpx; margin-top: 20rpx;">
						本周閲讀時長
					</view>
				</view>
				<view class="right">
					開通會員
				</view>
			</view>
			<view class="book-list">
				<view class="book-list-top">
					<view class="left" @click="goDetail(dayBook.sourceUrl)">
						<view class="img-box">
							<image :src="dayBook.cover" mode="widthFix"></image>
						</view>
						<view class="book-list-top-left-top">
							<view class="title">
								<view>今日推薦</view>
								<view class="img-box">
									<image src="../../static/fire.png" mode="widthFix"></image>
								</view>
							</view>
							<view class="book-title">
								{{dayBook.title}}
							</view>
							<view class="book-text">
								<text v-html="dayBook.description"></text>
							</view>

						</view>
					</view>
					<view class="right">
						<view>已签到</view>
						<view>{{signArr.length}}天</view>
						<view class="sign" v-if="!isSign" @click="sign()">
							签到
						</view>
						<view class="sign1" v-if="isSign">
							已签到
						</view>
					</view>
				</view>
				<view class="notice">
					<uni-notice-bar showIcon scrollable single background-color="FCECCC"
						text="每日更新好書，等你來讀！"></uni-notice-bar>
				</view>
				<view class="book-box">
					<view class="book" v-for="(item,index) in user.bookstore" @click="goDetail(item.id)">
						<view class="img-box">
							<image :src="item.link[1].href" mode="widthFix"></image>
						</view>
						<view class="book-title">
							{{item.title}}
						</view>
						<view class="author">
							{{item.author.name}}
						</view>
					</view>
					<view class="addBook" @click="goBookshelf()">
						<view class="img-box">
							<image src="../../static/addBook.png" mode="widthFix"></image>
						</view>
						<view>更多好書</view>
					</view>
				</view>


			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'bookstore',
		data() {
			return {
				dayBook: '',
				user: [],
				isSign: false,
				signArr: [],
			};
		},
		methods: {
			//每日推荐
			getDay() {
				uni.request({
					url: 'https://apis.netstart.cn/yunyuedu/column/data.json?id=2001',
					method: 'GET'
				}).then(res => {
					// console.log('res ==> ',res);
					this.dayBook = res.data.data.books[3]
					// console.log(this.dayBook);
					
				}).catch(err => {
					console.log('err ==> ', err);
				})
			},
			getBookstore() {
				let user = uni.getStorageSync('userInfo');
				if (!user) {
					return;
				}
				this.userInfo = JSON.parse(user);
				uniCloud.callFunction({
					name: 'getUser',
					data: {
						phone: this.userInfo.phone
					}
				}).then(res => {
					// console.log('res ==> ', res);
					this.user = res.result.data[0];
					this.signArr = res.result.data[0].sign
					// console.log('this.user ==> ',this.user);
					let date = new Date();
					let month = date.getMonth() + 1
					let day = date.getDate()
					let sign = month + '月' + day + '日'
					for(let i = 0; i < res.result.data[0].sign.length; i++){
						if(res.result.data[0].sign[i] == sign){
							 this.isSign = true
						}
					}
					uni.hideLoading()
				}).catch(err => {
					console.log('err ==> ', err);
				})
			},
			goBookshelf() {
				uni.switchTab({
					url: '../bookshelf/bookshelf'
				})
			},
			goDetail(id) {
				id = id.replace('/source/', '');
				let user = uni.getStorageSync('userInfo');
				if (!user) {
					uni.showToast({
						title: '未登录,请先去登录',
						icon: 'none'
					})
				} else {
					uni.navigateTo({
						url: '../detail/detail?id=' + id
					})
				}
			},
			goEditbook() {
				uni.navigateTo({
					url: '../editbook/editbook'
				})
			},
			sign() {
				let date = new Date();
				let month = date.getMonth() + 1
				let day = date.getDate()
				let sign = month + '月' + day + '日'
				let user = uni.getStorageSync('userInfo');
				if (!user) {
					console.log('没登陆');
					return;
				}
				// console.log('sign ==> ',sign);
				// console.log('this.userInfo ==> ',this.userInfo);
				this.isSign = true;
				this.signArr.push(sign)
				// console.log('this.signArr ==> ',this.signArr);
				uniCloud.callFunction({
					name: 'addSign',
					data: {
						phone: this.userInfo.phone,
						sign: this.signArr
					}
				}).then(res => {
					console.log('res ==> ', res);
					uni.showToast({
						title: '签到成功',
						icon: "none"
					})
				}).catch(err => {
					console.log('err ==> ', err);
				})

			}
		},
		onShow() {
			this.getDay();
			this.getBookstore();
			let userInfo = uni.getStorageSync('userInfo');
			if (!userInfo) {
				return
			}
			uni.showLoading({
				title: '正在加载中...'
			});
		},
	}
</script>

<style lang="scss">
	.bg {
		width: 100%;
		// height: 1234rpx;
		height: 100vh;
		background-image: url('../../static/background-image.png');
		background-repeat: no-repeat;
	}

	.top {
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 120rpx;

		.title {
			font-size: 40rpx;
			font-weight: 700;
			margin-left: 70rpx;
		}

		.edit {
			width: 40rpx;
			height: 40rpx;
			// background-color: red;
			margin-right: 50rpx;

			image {
				width: 100%;
				display: block;
			}
		}
	}

	.con {
		height: 170rpx;
		display: flex;
		justify-content: space-between;
		margin-left: 70rpx;
		align-items: flex-end;

		.con-top {
			display: flex;
			align-items: flex-end;

			.num {
				font-size: 100rpx;

			}
		}

		.right {
			width: 130rpx;
			height: 80rpx;
			// background-color: red;
			margin-right: 60rpx;
			background-image: url(../../static/sort.png);
			background-position: center;
			background-size: cover;
			background-repeat: no-repeat;
			font-size: 24rpx;
			line-height: 72rpx;
			text-align: center;
			color: white;
		}
	}

	.book-list {
		width: 680rpx;
		min-height: 1000rpx;
		margin: 30rpx auto;
		background-color: #FCECCC;
		border-radius: 30rpx;
		padding-bottom: 100rpx;

		.book-list-top {
			height: 240rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.left {
				display: flex;
				width: 480rpx;
				height: 180rpx;
				margin-left: 10rpx;

				// justify-content: center;
				.img-box:nth-child(1) {
					width: 120rpx;
					height: 160rpx;
					margin-left: 10rpx;

					image {
						width: 100%;
						display: block;
						border-radius: 20rpx;
					}
				}

				.book-list-top-left-top {
					.title {
						height: 30rpx;
						font-size: 28rpx;
						color: #ee9c6f;
						display: flex;
						align-items: center;
						margin-left: 20rpx;

						.img-box {
							width: 26rpx;
							margin-left: 10rpx;

							image {
								width: 100%;
								display: block;
							}
						}
					}

					.book-title {
						font-size: 32rpx;
						font-weight: 700;
						margin-left: 20rpx;
						margin-top: 20rpx;
					}

					.book-text {
						width: 300rpx;
						font-size: 24rpx;
						color: #878787;
						margin-left: 20rpx;
						margin-top: 20rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
					}


				}

			}

			.right {
				width: 166rpx;
				height: 180rpx;
				// background-color: red;
				border-left: 1px solid #FC768A;
				margin-right: 20rpx;
				line-height: 60rpx;
				text-align: center;
				font-weight: 700;

				.sign {
					width: 130rpx;
					height: 56rpx;
					border-radius: 30rpx;
					background-color: #EB732A;
					text-align: center;
					line-height: 56rpx;
					color: white;
					font-size: 30rpx;
					margin-left: 24rpx;
					font-weight: normal;
				}

				.sign1 {
					width: 130rpx;
					height: 56rpx;
					border-radius: 30rpx;
					background-color: #ccc;
					text-align: center;
					line-height: 56rpx;
					color: #fff;
					font-size: 30rpx;
					margin-left: 24rpx;
					font-weight: normal;
				}
			}
		}

		.book-box {
			display: flex;
			flex-wrap: wrap;

			.book {
				width: 200rpx;
				height: 370rpx;
				// border: 1px solid #EB732A;
				margin-left: 16rpx;
				margin-top: 16rpx;

				// background-color: yellow;
				.img-box {
					width: 200rpx;
					height: 260rpx;
					border: 1px solid #EB732A;
					border-radius: 20rpx;

					image {
						width: 100%;
						display: block;
						border-radius: 20rpx;
					}
				}

				.book-title {
					font-size: 32rpx;
					font-weight: 700;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 1;
					-webkit-box-orient: vertical;
					margin-top: 10rpx;
				}

				.author {
					font-size: 26rpx;
					color: #a8a8a8;
					margin-top: 8rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 1;
					-webkit-box-orient: vertical;
				}
			}

			.addBook {
				width: 200rpx;
				height: 250rpx;
				border: 1px solid #EB732A;
				margin-left: 16rpx;
				margin-top: 16rpx;
				text-align: center;
				color: #e89330;

				.img-box {
					width: 60rpx;
					height: 60rpx;
					// background-color: red;
					margin: 0 auto;
					margin-top: 60rpx;
					margin-bottom: 40rpx;

					image {
						width: 100%;
						display: block;
					}
				}
			}
		}
	}
</style>